package com.taskcollection.control;

import com.alibaba.fastjson.JSONObject;
import com.taskcollection.bean.ProjectTeamBean;
import com.taskcollection.bean.StudentBean;
import com.taskcollection.bean.TeacherBean;
import com.taskcollection.service.ProjectTeamService;
import com.taskcollection.service.StudentProjectService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by haocongshun on 2016/8/18.
 */
public class ProjectTeamControl {
    public void projectTeam(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String flag = request.getParameter("flag");
        ProjectTeamService pts = new ProjectTeamService();
        JSONObject json = new JSONObject();
        if("getAll".equals(flag)){
            String teacher_id = ((TeacherBean)request.getSession().getAttribute("教师")).getTeacher_id();
            List<ProjectTeamBean> projectTeamList = pts.getAll(teacher_id);
            json.put("getAll",projectTeamList);
            System.out.println(json.toJSONString());
        }
        if("see".equals(flag)){
            String projectTeam_id = request.getParameter("projectTeamID");
            StudentProjectService sps = new StudentProjectService();
            List<StudentBean> studentList = sps.getByProjectTeamId(projectTeam_id);
            ProjectTeamBean ptb = pts.getById(projectTeam_id);
            json.put("see",studentList);
            json.put("projectTeam",ptb);
            System.out.println(json.toJSONString());
        }
        if("leader".equals(flag)){
            String leaderID = request.getParameter("leaderID");
            String projectTeamID = request.getParameter("projectTeamID");
            ProjectTeamBean ptb = pts.getById(projectTeamID);
            ptb.setProjectTeam_leader(leaderID);
            boolean temp = pts.change(ptb);
            if(temp==true){
                if("0".equals(leaderID)){
                    json.put("msg","设为组长");
                }else {
                    json.put("msg","取消组长");
                }
            }else {
                json.put("msg",temp);
            }
            System.out.println(json);
        }
        response.getWriter().print(json.toJSONString());
    }

}
